/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import model.Examen;
import model.Cour;

/**
 *
 * @author Paul
 */
public class DAOExamen extends DAO<Examen> {

    public Examen find(int id) {
        String query = "";
        Examen examen = null;
        examen = findGeneral(query).get(0);
        return examen;
    }

    public void create(Examen obj) {
        String query="";
        changeGeneral(query);
    }

    public void update(Examen obj) {
        String query="";
        changeGeneral(query);
    }

    public void delete(Examen obj) {
        String query="";
        changeGeneral(query);
    }

    public ArrayList<Examen> getAll() {
        String query ="";
        return findGeneral(query);
    }

    public ArrayList<Examen> getExamFromCour(int id_cour) {
        String query ="select e.id_examen,e.nom from examen e, cour_exam_asso cea where cea.id_examen=e.id_examen and cea.id_cour="+id_cour+";";
        return findGeneral(query);
    }

    
    
    
    
    
    private int changeGeneral(String query){
               Statement st = null;
               int nb=0;
       try {
           Connection conn = DBConnection.getConnection();
           st = conn.createStatement();
           nb = st.executeUpdate(query);
       } catch (SQLException e) {
           e.printStackTrace();
       }
        return nb;
    }
    
    private ArrayList<Examen> findGeneral(String requete) {
        ArrayList<Examen> examens = new ArrayList<Examen>();
        Statement st = null;
        Examen examen = null;

        try {
            Connection conn = DBConnection.getConnection();
            st = conn.createStatement();
            ResultSet res = st.executeQuery(requete);
            while (res.next()) {
                examen = createRes(res);
                examens.add(examen);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return examens;
    }

    private Examen createRes(ResultSet res) {
        Examen examen = null;
        try {
            int idE = res.getInt("id_examen");
            String nom = res.getString("nom");
            examen = new Examen(idE, nom);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return examen;
    }
}
